
/**
 * 游戏通信连接
 */
export abstract class GameConn {

    private _cbMap = new Map<KProtoId, { cb: (data: any, protoId: KProtoId) => any, caller: any }>();

    /**
     * 监听协议
     * @param protoId 
     * @param cb 
     * @param caller 
     */
    on<ID extends KProtoTosNeed>(protoId: ID, cb: (data: KProtoTos[ID], protoId: KProtoId) => any, caller?: any): void
    on<ID extends KProtoTosNever>(protoId: ID, cb: (data: void, protoId: KProtoId) => any, caller?: any): void
    on(protoId: KProtoId, cb: (data: any, protoId: KProtoId) => any, caller?: any): void {
        this._cbMap.set(protoId, { cb, caller });
    }

    /**
     * 接收到协议
     * @param protoId 
     * @param data 
     */
    onProto(protoId: KProtoId, data?: any): void {
        const cb = this._cbMap.get(protoId);
        if (cb) {
            cb.cb.call(cb.caller, data, protoId);
        }
    }

    /**
     * 发送协议
     * @param protoId 
     * @param data 
     */
    abstract send<ID extends KProtoTocNeed>(protoId: ID, data: KProtoToc[ID]): void;
    abstract send<ID extends KProtoTocNeed>(protoId: ID): void;

}